package com.szbber.medical.viewmodel.fragment

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.orhanobut.logger.Logger
import com.szbber.commen.api.Repository
import com.szbber.commen.api.launch
import com.szbber.commen.base.BaseViewModel
import com.szbber.commen.bean.*
import com.szbber.commen.dialog.DialogUtils
import com.szbber.commen.utils.PreferUtil
import com.szbber.commen.utils.dataStoreRead
import com.szbber.commen.utils.dataStoreSave
import kotlinx.android.synthetic.main.fragment_port_setting.*

/**
 * @author: Administrator
 * @date: 2022/4/19
 */
class PortSettingViewModel : BaseViewModel() {

    var deviceType = MutableLiveData<String>()
    fun getDeviceType(context: Context?) = launch(context, {
        when (dataStoreRead<Int>("DeviceType")) {
            0 -> deviceType.value = "未设置设备"
            1 -> deviceType.value = "床头屏设备"
            2 -> deviceType.value = "门口屏设备"
            3 -> deviceType.value = "走廊屏设备"
            4 -> deviceType.value = "护士站大屏设备"
            5 -> deviceType.value = "电话机设备"
        }
    })

    fun setDeviceType(context: Context?, value: Int) = launch(context, {
        dataStoreSave<Int>("DeviceType",value)
        when (value) {
            0 -> deviceType.value = "未设置设备"
            1 -> deviceType.value = "床头屏设备"
            2 -> deviceType.value = "门口屏设备"
            3 -> deviceType.value = "走廊屏设备"
            4 -> deviceType.value = "护士站大屏设备"
            5 -> deviceType.value = "电话机设备"
        }
    })

    var serviceIP = MutableLiveData<String>()
    fun getServiceIP(context: Context?) = launch(context, {
        serviceIP.value = dataStoreRead<String>("serviceIp")
    })

    fun setServiceIP(tag: String) = launch {
        dataStoreSave<String>("serviceIp", tag)
        serviceIP.value = tag
    }

    fun getHospitalBean(context: Context?) = launch(context, {
        val data = Repository.getHospitalBean(context)
        DialogUtils.showHospitalInfoDialog(context, data) {
            launch { dataStoreSave<String>("hospitalBean", Gson().toJson(it.hospitalBean)) }
            hospitalInfo.value = it.hospitalBean
        }
    })

    var hospitalInfo = MutableLiveData<HospitalBean.Data>()
    fun getHospitalInfo() = launch {
        val data = dataStoreRead<String>("hospitalBean")
        val bean = Gson().fromJson<HospitalBean.Data>(data, HospitalBean.Data::class.java)
        hospitalInfo.value = bean
    }

    fun getDeptBean(context: Context?) = launch(context, {
        val data = Repository.getDeptBean(context)
        DialogUtils.showDeptBeanDialog(context, data) {
            launch {
                dataStoreSave<String>("DeptBean", Gson().toJson(it.deptBean))
                it.deptBean?.code?.apply { dataStoreSave<String>("deptCode", this) }
            }
            deptInfo.value = it.deptBean
        }
    })

    var deptInfo = MutableLiveData<DeptBean.Data>()
    fun getDeptInfo() = launch {
        val data = dataStoreRead<String>("DeptBean")
        val bean = Gson().fromJson<DeptBean.Data>(data, DeptBean.Data::class.java)
        deptInfo.value = bean
    }

    fun getDistrictBean(context: Context?) = launch(context, {
        val data = Repository.getDistrictBean(context)
        DialogUtils.showDistrictDialog(context, data) {
            launch {
                dataStoreSave<String>("DistrictInfo", Gson().toJson(it.districtBean))
                it.districtBean?.code?.apply { dataStoreSave<String>("districtCode", this) }
            }
            districtInfo.value = it.districtBean
        }
    })

    var districtInfo = MutableLiveData<DistrictBean.Data>()
    fun getDistrictInfo() = launch {
        val data = dataStoreRead<String>("DistrictInfo")
        val bean = Gson().fromJson<DistrictBean.Data>(data, DistrictBean.Data::class.java)
        districtInfo.value = bean
    }

    fun getRoomBean(context: Context?) = launch(context, {
        val deptCode = dataStoreRead<String>("deptCode")
        val districtCode = dataStoreRead<String>("districtCode")
        val queue = arrayOf("deptCode", deptCode, "districtCode", districtCode)
        val data = Repository.getRoomBean(context, *queue)
        DialogUtils.showRoomBeanDialog(context, data) {
            launch {
                dataStoreSave<String>("RoomInfo", Gson().toJson(it.roomBean))
                it.roomBean?.code?.apply { dataStoreSave<String>("roomCode", this) }
            }
            roomInfo.value = it.roomBean
        }
    })

    var roomInfo = MutableLiveData<RoomBean.Data>()
    fun getRoomInfo() = launch {
        val data = dataStoreRead<String>("RoomInfo")
        val bean = Gson().fromJson<RoomBean.Data>(data, RoomBean.Data::class.java)
        roomInfo.value = bean
    }

    fun getBedBean(context: Context?) = launch(context, {
        val deptCode = dataStoreRead<String>("deptCode")
        val queue = arrayOf("deptCode", deptCode)
        val data = Repository.getBedBean(context, *queue)
        DialogUtils.showBedBeanDialog(context, data) {
            launch {
                dataStoreSave<String>("BedInfo", Gson().toJson(it.bedBean))
                it.bedBean?.bed_code?.apply { dataStoreSave<String>("bedCode", this) }
            }
            bedInfo.value = it.bedBean
        }
    })

    var bedInfo = MutableLiveData<BedBean.Data>()
    fun getBedInfo() = launch {
        val data = dataStoreRead<String>("BedInfo")
        val bean = Gson().fromJson<BedBean.Data>(data, BedBean.Data::class.java)
        bedInfo.value = bean
    }

}